Get Serial Number
id: get_serial_number title: Get Serial Number sidebar_label: Get Serial Number position: 1
Description
The get_serial_number
function retrieves the serial number of the CPU based on the operating system. If the system is Windows, it calls the get_cpu_serial_number_windows()
function. If the system is Linux, it calls the get_cpu_serial_number_linux()
function. If the operating system is neither Windows nor Linux, it returns an empty string.
Function Signature:
def get_serial_number() -> str:
Parameters
This function does not take any parameters.
Returns
- str: Returns the CPU serial number as a string. If there is an error, it returns
"serial_error"
. If the system is not Windows or Linux, it returns an empty string.
Example Usage
Example without error:
If the function successfully retrieves the serial number:
serial = get_serial_number()
print(serial)
Output:
<serial_number>
Example with error:
If there is an error (e.g., in case of unsupported OS or failure to get serial number):
serial = get_serial_number()
print(serial)
Output:
serial_error
Code
import platform
def get_serial_number() -> str:
try:
serial_number = ""
if platform.system() == "Windows":
serial_number = get_cpu_serial_number_windows()
elif platform.system() == "Linux":
serial_number = get_cpu_serial_number_linux()
else:
serial_number = ""
return serial_number
except:
return "serial_error"
Errors and Exceptions
- If the function encounters an issue while retrieving the serial number, it will catch the error and return
"serial_error"
. - This function does not handle specific exceptions explicitly, so it will return
"serial_error"
for any error that occurs.
Notes
- This function relies on platform-specific methods to get the CPU serial number. Ensure that
get_cpu_serial_number_windows()
andget_cpu_serial_number_linux()
are defined in the code. - If the function is unable to retrieve the serial number due to an unsupported platform or any other error, it will return
"serial_error"
.